package org.edu.modules.sxjy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.edu.common.api.vo.Result;
import org.edu.modules.sxjy.entity.VeApply;
import org.edu.modules.sxjy.entity.VeAttendance;
import org.edu.modules.sxjy.entity.VeClassTeacher;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VeDynamic;
import org.edu.modules.sxjy.entity.VePractice;
import org.edu.modules.sxjy.entity.VeStudent;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.mapper.VeAttendanceMapper;
import org.edu.modules.sxjy.service.IVeApplyService;
import org.edu.modules.sxjy.service.IVeAttendanceService;
import org.edu.modules.sxjy.service.IVeClassTeacharService;
import org.edu.modules.sxjy.service.IVeCompanyUserService;
import org.edu.modules.sxjy.service.IVeDynamicService;
import org.edu.modules.sxjy.service.IVePracticeService;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.VeAttendanceAddVo;
import org.edu.modules.sxjy.vo.VeAttendanceInfoVo;
import org.edu.modules.sxjy.vo.VeAttendanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeAttendanceServiceImpl
        extends ServiceImpl<VeAttendanceMapper, VeAttendance>
        implements IVeAttendanceService
{
    @Autowired
    private VeAttendanceMapper veAttendanceMapper;
    @Autowired
    private IVeAttendanceService veAttendanceService;
    @Autowired
    private IVePracticeService vePracticeService;
    @Autowired
    private VeCommonClient veCommonClient;
    @Autowired
    private IVeCompanyUserService veCompanyUserService;
    @Autowired
    private IVeClassTeacharService veClassTeacharService;
    @Autowired
    private IVeDynamicService veDynamicService;
    @Autowired
    private IVeApplyService veApplyService;

    public List<VeAttendanceVo> selectAll(VeAttendanceVo veAttendanceVo, Integer startLine, Integer pageSize, VeCommon veCommon)
    {
        List<VeAttendanceVo> veAttendanceVoList = this.veAttendanceMapper.selectAll(veAttendanceVo, startLine, pageSize, veCommon);
        VeCompanyUser veCompanyUser;
        if (veCommon.getUserType().contains("0"))
        {
            veAttendanceVoList = this.veAttendanceMapper.selectAll(veAttendanceVo, startLine, pageSize, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                veAttendanceVoList = this.veAttendanceMapper.selectAll(veAttendanceVo, startLine, pageSize, veCommon);
            } else {
                veAttendanceVoList = new ArrayList();
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                veAttendanceVoList = this.veAttendanceMapper.selectAll(veAttendanceVo, startLine, pageSize, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            veAttendanceVoList = this.veAttendanceMapper.selectAll(veAttendanceVo, startLine, pageSize, veCommon);
        }
        if ((veAttendanceVoList != null) && (veAttendanceVoList.size() > 0)) {
            for (VeAttendanceVo attendanceVo : veAttendanceVoList)
            {
                if (attendanceVo.getStatus().equals("1")) {
                    attendanceVo.setStatus("已安排");
                } else if (attendanceVo.getStatus().equals("2")) {
                    attendanceVo.setStatus("换岗");
                } else if (attendanceVo.getStatus().equals("3")) {
                    attendanceVo.setStatus("已结束");
                }
                int tardy = this.veAttendanceMapper.getTardy(attendanceVo.getStuUserId());
                if (tardy != 0) {
                    attendanceVo.setTardy(tardy);
                }
                int vacation = this.veAttendanceMapper.getVacation(attendanceVo.getStuUserId());
                if (vacation != 0) {
                    attendanceVo.setVacation(vacation);
                }
                int absend = this.veAttendanceMapper.getAbsend(attendanceVo.getStuUserId());
                if (absend != 0) {
                    attendanceVo.setAbsend(absend);
                }
                VePractice vePractice = this.veAttendanceMapper.getPracticeByStuUserId(attendanceVo.getStuUserId());
                if ((vePractice != null) && (!"".equals(vePractice)))
                {
                    attendanceVo.setCpName(vePractice.getCpName());
                    attendanceVo.setPostName(vePractice.getPostName());
                    VeApply veApply = (VeApply)this.veApplyService.getById(Integer.valueOf(vePractice.getApplyId()));
                    if (veApply.getType() == 2) {
                        attendanceVo.setStuIsFocusApper(2);
                    } else if (veApply.getType() == 1) {
                        attendanceVo.setStuIsFocusApper(1);
                    }
                }
            }
        }
        return veAttendanceVoList;
    }

    public int getSumPage(VeAttendanceVo veAttendanceVo, VeCommon veCommon)
    {
        int pageNum = 0;
        if (veCommon.getUserType().contains("0"))
        {
            pageNum = this.veAttendanceMapper.getSumPage(veAttendanceVo, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                pageNum = this.veAttendanceMapper.getSumPage(veAttendanceVo, veCommon);
            } else {
                pageNum = 0;
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                pageNum = this.veAttendanceMapper.getSumPage(veAttendanceVo, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            VeCompanyUser veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            pageNum = this.veAttendanceMapper.getSumPage(veAttendanceVo, veCommon);
        }
        return pageNum;
    }

    public List<VeAttendanceInfoVo> getOneInfo(Integer id, String userId, Integer type)
    {
        List<VeAttendanceInfoVo> veAttendanceInfoVoList = new ArrayList();

        VePractice vePractice = (VePractice)this.vePracticeService.getById(id);
        if ((vePractice == null) || ("".equals(vePractice))) {
            return veAttendanceInfoVoList;
        }
        veAttendanceInfoVoList = this.veAttendanceMapper.getAttendInfoVoListByStuUserId(vePractice.getStuUserId(), type);
        if ((veAttendanceInfoVoList != null) && (veAttendanceInfoVoList.size() > 0)) {
            for (VeAttendanceInfoVo veAttendanceInfoVo : veAttendanceInfoVoList)
            {
                veAttendanceInfoVo.setXh(vePractice.getXh());

                veAttendanceInfoVo.setXm(vePractice.getXm());

                veAttendanceInfoVo.setStartTime(DateUtils.getDateToString(Long.parseLong(veAttendanceInfoVo.getStartTime())));

                veAttendanceInfoVo.setEndTime(DateUtils.getDateToString(Long.parseLong(veAttendanceInfoVo.getEndTime())));
                if (veAttendanceInfoVo.getTypeId() == 1) {
                    veAttendanceInfoVo.setType("迟到");
                } else if (veAttendanceInfoVo.getTypeId() == 2) {
                    veAttendanceInfoVo.setType("早退");
                } else if (veAttendanceInfoVo.getTypeId() == 3) {
                    veAttendanceInfoVo.setType("旷工");
                } else if (veAttendanceInfoVo.getTypeId() == 4) {
                    veAttendanceInfoVo.setType("病假");
                } else if (veAttendanceInfoVo.getTypeId() == 5) {
                    veAttendanceInfoVo.setType("事假");
                } else if (veAttendanceInfoVo.getTypeId() == 6) {
                    veAttendanceInfoVo.setType("公出");
                }
                veAttendanceInfoVo.setCpName(vePractice.getCpName());

                veAttendanceInfoVo.setPostName(vePractice.getPostName());
            }
        }
        return veAttendanceInfoVoList;
    }

    public List<VeAttendanceInfoVo> getOneInfoXls(VeCommon veCommon)
    {
        List<VeAttendanceInfoVo> veAttendanceInfoVoList = new ArrayList();

        VePractice vePractice = (VePractice)this.vePracticeService.getById(veCommon.getId());
        if ((vePractice == null) || ("".equals(vePractice))) {
            return veAttendanceInfoVoList;
        }
        veAttendanceInfoVoList = this.veAttendanceMapper.getAttendInfoVoListByStuUserId(vePractice.getStuUserId(), veCommon.getTypeXls());
        if ((veAttendanceInfoVoList != null) && (veAttendanceInfoVoList.size() > 0)) {
            for (VeAttendanceInfoVo veAttendanceInfoVo : veAttendanceInfoVoList)
            {
                veAttendanceInfoVo.setXh(vePractice.getXh());

                veAttendanceInfoVo.setXm(vePractice.getXm());

                veAttendanceInfoVo.setStartTime(DateUtils.getDateToString(Long.parseLong(veAttendanceInfoVo.getStartTime())));

                veAttendanceInfoVo.setEndTime(DateUtils.getDateToString(Long.parseLong(veAttendanceInfoVo.getEndTime())));
                if (veAttendanceInfoVo.getTypeId() == 1) {
                    veAttendanceInfoVo.setType("迟到");
                } else if (veAttendanceInfoVo.getTypeId() == 2) {
                    veAttendanceInfoVo.setType("早退");
                } else if (veAttendanceInfoVo.getTypeId() == 3) {
                    veAttendanceInfoVo.setType("旷工");
                } else if (veAttendanceInfoVo.getTypeId() == 4) {
                    veAttendanceInfoVo.setType("病假");
                } else if (veAttendanceInfoVo.getTypeId() == 5) {
                    veAttendanceInfoVo.setType("事假");
                } else if (veAttendanceInfoVo.getTypeId() == 6) {
                    veAttendanceInfoVo.setType("公出");
                }
                veAttendanceInfoVo.setCpName(vePractice.getCpName());

                veAttendanceInfoVo.setPostName(vePractice.getPostName());
            }
        }
        return veAttendanceInfoVoList;
    }

    public Result<?> saves(VeAttendanceAddVo veAttendanceAddVo)
    {
        VeAttendance veAttendance = new VeAttendance();


        veAttendance.setTypeId(veAttendanceAddVo.getType());

        veAttendance.setStartTime(DateUtils.getStringToDate(veAttendanceAddVo.getStartTime()));

        veAttendance.setEndTime(DateUtils.getStringToDate(veAttendanceAddVo.getEndTime()));
        if (String.valueOf(veAttendanceAddVo.getDay()).length() > 5) {
            return Result.error("天数过大");
        }
        veAttendance.setDay(veAttendanceAddVo.getDay());


        veAttendance.setRemark(veAttendanceAddVo.getRemark());



        VePractice vePractices = (VePractice)this.vePracticeService.getById(Integer.valueOf(veAttendanceAddVo.getId()));
        veAttendance.setStuUserId(vePractices.getStuUserId());







        veAttendance.setPrcId(veAttendanceAddVo.getId());
        if (veAttendanceAddVo.getUserType().equals(Integer.valueOf(1)))
        {
            Object teacherResult = this.veCommonClient.queryTeacherByUserId("09d5e1e7f9b049008eee645c783a1d67", veAttendanceAddVo.getUserId()).getResult();
            Map teacherMap = (Map)teacherResult;
            if (teacherMap != null) {
                veAttendance.setCreateUserId(Integer.parseInt(teacherMap.get("id").toString()));
            }
        }
        else if (veAttendanceAddVo.getUserType().equals(Integer.valueOf(4)))
        {
            VeCompanyUser veCompanyUser = this.veAttendanceMapper.getCpUserIdByUserId(veAttendanceAddVo.getUserId());
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser))) {
                veAttendance.setCreateUserId(veCompanyUser.getId());
            }
        }
        veAttendance.setCreateTime(System.currentTimeMillis());

        veAttendance.setTerminalId(1);


        this.veAttendanceService.save(veAttendance);




        VeDynamic veDynamic = new VeDynamic();


        veDynamic.setStuUserId(vePractices.getStuUserId());


        veDynamic.setCreateTime(System.currentTimeMillis());

        veDynamic.setCreateUserId(1);

        veDynamic.setPrcId(vePractices.getId());

        veDynamic.setPostId(vePractices.getPostId());

        veDynamic.setPostName(vePractices.getPostName());

        veDynamic.setCpId(vePractices.getCpId());

        veDynamic.setCpName(vePractices.getCpName());
        if (veAttendanceAddVo.getType() == 1) {
            veDynamic.setRemark("迟到");
        } else if (veAttendanceAddVo.getType() == 2) {
            veDynamic.setRemark("早退");
        } else if (veAttendanceAddVo.getType() == 3) {
            veDynamic.setRemark("旷工");
        } else if (veAttendanceAddVo.getType() == 4) {
            veDynamic.setRemark("病假");
        } else if (veAttendanceAddVo.getType() == 5) {
            veDynamic.setRemark("事假");
        } else if (veAttendanceAddVo.getType() == 6) {
            veDynamic.setRemark("公出");
        }
        veDynamic.setTerminalId(1);

        this.veDynamicService.save(veDynamic);

        return Result.OK("添加成功");
    }

    public List<VeAttendanceInfoVo> selectStuInfo(String userId, int typeId)
    {
        Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", userId).getResult();
        Map stuMap = (Map)stuResult;
        String s = JSON.toJSONString(stuMap);
        VeStudent veStudent = (VeStudent)JSON.parseObject(s, VeStudent.class);


        List<VeAttendanceInfoVo> veAttendanceInfoVoList = new ArrayList();
        if ((veStudent == null) || ("".equals(veStudent))) {
            return veAttendanceInfoVoList;
        }
        List<VeAttendance> veAttendanceLists = this.veAttendanceMapper.getAttendanceByStuId(veStudent.getId());
        for (VeAttendance attendanceList : veAttendanceLists) {
            if (typeId != 100)
            {
                if (typeId == attendanceList.getTypeId())
                {
                    VeAttendanceInfoVo veAttendanceInfoVo = new VeAttendanceInfoVo();

                    veAttendanceInfoVo.setId(attendanceList.getId());
                    if ((veStudent != null) && (!"".equals(veStudent)))
                    {
                        veAttendanceInfoVo.setXm(veStudent.getXm());

                        veAttendanceInfoVo.setXh(veStudent.getXh());
                    }
                    veAttendanceInfoVo.setStartTime(DateUtils.getDateToString(attendanceList.getStartTime()));

                    veAttendanceInfoVo.setEndTime(DateUtils.getDateToString(attendanceList.getEndTime()));

                    veAttendanceInfoVo.setDay(attendanceList.getDay());
                    if (attendanceList.getTypeId() == 1) {
                        veAttendanceInfoVo.setType("迟到");
                    } else if (attendanceList.getTypeId() == 2) {
                        veAttendanceInfoVo.setType("早退");
                    } else if (attendanceList.getTypeId() == 3) {
                        veAttendanceInfoVo.setType("旷工");
                    } else if (attendanceList.getTypeId() == 4) {
                        veAttendanceInfoVo.setType("病假");
                    } else if (attendanceList.getTypeId() == 5) {
                        veAttendanceInfoVo.setType("事假");
                    } else if (attendanceList.getTypeId() == 6) {
                        veAttendanceInfoVo.setType("公出");
                    } else {
                        Result.error("无状态，有误");
                    }
                    String cpName = ((VePractice)this.vePracticeService.getById(Integer.valueOf(attendanceList.getPrcId()))).getCpName();
                    if ((cpName != null) && (!"".equals(cpName))) {
                        veAttendanceInfoVo.setCpName(cpName);
                    }
                    String postName = ((VePractice)this.vePracticeService.getById(Integer.valueOf(attendanceList.getPrcId()))).getPostName();
                    if ((postName != null) && (!"".equals(postName))) {
                        veAttendanceInfoVo.setPostName(postName);
                    }
                    veAttendanceInfoVo.setRemark(attendanceList.getRemark());

                    veAttendanceInfoVoList.add(veAttendanceInfoVo);
                }
            }
            else
            {
                VeAttendanceInfoVo veAttendanceInfoVo = new VeAttendanceInfoVo();

                veAttendanceInfoVo.setId(attendanceList.getId());
                if ((veStudent != null) && (!"".equals(veStudent)))
                {
                    veAttendanceInfoVo.setXm(veStudent.getXm());

                    veAttendanceInfoVo.setXh(veStudent.getXh());
                }
                veAttendanceInfoVo.setStartTime(DateUtils.getDateToString(attendanceList.getStartTime()));

                veAttendanceInfoVo.setEndTime(DateUtils.getDateToString(attendanceList.getEndTime()));

                veAttendanceInfoVo.setDay(attendanceList.getDay());
                if (attendanceList.getTypeId() == 1) {
                    veAttendanceInfoVo.setType("迟到");
                } else if (attendanceList.getTypeId() == 2) {
                    veAttendanceInfoVo.setType("早退");
                } else if (attendanceList.getTypeId() == 3) {
                    veAttendanceInfoVo.setType("旷工");
                } else if (attendanceList.getTypeId() == 4) {
                    veAttendanceInfoVo.setType("病假");
                } else if (attendanceList.getTypeId() == 5) {
                    veAttendanceInfoVo.setType("事假");
                } else if (attendanceList.getTypeId() == 6) {
                    veAttendanceInfoVo.setType("公出");
                } else {
                    Result.error("无状态，有误");
                }
                String cpName = ((VePractice)this.vePracticeService.getById(Integer.valueOf(attendanceList.getPrcId()))).getCpName();
                if ((cpName != null) && (!"".equals(cpName))) {
                    veAttendanceInfoVo.setCpName(cpName);
                }
                String postName = ((VePractice)this.vePracticeService.getById(Integer.valueOf(attendanceList.getPrcId()))).getPostName();
                if ((postName != null) && (!"".equals(postName))) {
                    veAttendanceInfoVo.setPostName(postName);
                }
                veAttendanceInfoVo.setRemark(attendanceList.getRemark());

                veAttendanceInfoVoList.add(veAttendanceInfoVo);
            }
        }
        return veAttendanceInfoVoList;
    }
}
